package edu.wtbu.cs.androiddemo.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.ArrayList;
import java.util.List;

import edu.wtbu.cs.androiddemo.MainActivity;
import edu.wtbu.cs.androiddemo.R;
import edu.wtbu.cs.androiddemo.sqlite.SQLiteHelper;
import edu.wtbu.cs.androiddemo.sqlite.UserInfo;

public class SQLiteDemoActivity extends AppCompatActivity {

    String logTag = SQLiteDemoActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_sqlite_demo);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
       // SQLiteHelper sqLiteHelper = new SQLiteHelper(this);
       // SQLiteDatabase database = sqLiteHelper.getWritableDatabase();
        insert("chende","pwd","ccdedeguo");
    }

    public void clickSQLiteButton(View view){
        Log.i(logTag,"clickSQLiteButton");
        Toast.makeText(SQLiteDemoActivity.this,"您选择了clickSQLiteButton",Toast.LENGTH_LONG).show();
    }

    public void clickRoomButton(View view){
        Log.i(logTag,"clickRoomButton");
        Intent intent = new Intent(SQLiteDemoActivity.this, RoomSampleActivity.class);
        startActivity(intent);
    }

    public void insert(String name, String pwd, String nick) {
        SQLiteHelper sqLiteHelper = new SQLiteHelper(this);
        SQLiteDatabase database = sqLiteHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("user_name", name);
        values.put("user_pwd", pwd);
        values.put("user_nick", nick);
        long id = database.insert("userinfo", null, values);

        database.close();
    }

    public int delete(String name) {
        SQLiteHelper sqLiteHelper = new SQLiteHelper(this);
        SQLiteDatabase database = sqLiteHelper.getWritableDatabase();
        int num = database.delete("userinfo", "user_name=?", new String[]{name});
        database.close();
        return num;
    }

    public int update(String name, String pwd, String nick) {
        SQLiteHelper sqLiteHelper = new SQLiteHelper(this);
        SQLiteDatabase database = sqLiteHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("user_pwd", pwd);
        values.put("user_nick", nick);
        int num = database.update("userinfo", values, "user_name=?", new String[]{name});
        database.close();
        return num;
    }

    public List<UserInfo> query(String name) {
        List<UserInfo> infoList = new ArrayList<UserInfo>();
        SQLiteHelper sqLiteHelper = new SQLiteHelper(this);
        SQLiteDatabase database = sqLiteHelper.getWritableDatabase();
        Cursor cursor = database.query("userinfo", null, "user_name=?", new String[]{name}, null,
                null, null, null);
        if (cursor != null && cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                UserInfo info = new UserInfo();
                info.setUser_name(cursor.getString(1));
                info.setUser_pwd(cursor.getString(2));
                info.setUser_nick(cursor.getString(3));
                infoList.add(info);
            }
        }
        cursor.close();
        database.close();
        return infoList;
    }
}